package com.gdk.ksoft.ui.mores;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.assembly.dialog.ExitDialog;
import com.gdk.ksoft.assembly.utils.system.ApkUtils;
import com.gdk.ksoft.assembly.utils.UpdateUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * Created by hel on 2014-11-21.
 * 工作页面
 */
public class FragmentMore extends Fragment {

    /**
     * 退出程序监听器
     */
    public interface OnExitListener{
        public void onExit();
    }

    @ViewInject(R.id.fmore_item_txtCheckUpdate)
    private TextView txtCheckUpdate;//检测更新
    public static final String TAG = FragmentMore.class.getSimpleName();

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(activity!=null)
            exitListener = (OnExitListener)activity;
        if(exitListener == null){
            Log.e(TAG,"FragmentMore所在的Activity未能实现OnExitListener监听器!");
        }
    }

    private OnExitListener exitListener;//退出程序服务监听器

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_more,null);
        ViewUtils.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        txtCheckUpdate.setText(Html.fromHtml("版本更新<b><font color='#808080'>(" + ApkUtils.getVersionName(getActivity())+")</font></b>"));
    }

    @OnClick(value={R.id.fmore_item_txtSettings,R.id.fmore_item_txtCheckUpdate,
            R.id.fmore_item_txtAboutUs,R.id.fmore_item_txtExitProgam,
            R.id.fmore_item_txtExplain,R.id.fmore_item_txtVersionIntroduce})
    public void onItemClick(View view){
        switch(view.getId()){
            case R.id.fmore_item_txtSettings://软件设置
                startActivity(new Intent(getActivity(),SoftwareSettingsActivity.class));
                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case R.id.fmore_item_txtVersionIntroduce://版本特性
                startActivity(new Intent(getActivity(),VersionIntroduceActivity.class));
                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case R.id.fmore_item_txtCheckUpdate://检查更新
                Toast.makeText(getActivity(),"正在检测...",Toast.LENGTH_SHORT).show();
                UpdateUtils.checkVersion(getActivity(), true);//检测更新
                break;
            case R.id.fmore_item_txtAboutUs://关于我们
                startActivity(new Intent(getActivity(),CompanyActivity.class));
                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case R.id.fmore_item_txtExplain://意见反馈
                Toast.makeText(getActivity(),"暂不支持!",Toast.LENGTH_SHORT).show();
//                startActivity(new Intent(getActivity(),ExplainActivity.class));
//                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case R.id.fmore_item_txtExitProgam://退出程序
                final ExitDialog dialog = new ExitDialog(getActivity());
                dialog.OnPositiveNegitiveClick(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        if(v.getId() == R.id.dexit_txtOK && exitListener!=null){
                            exitListener.onExit();
                        }
                    }
                });
                dialog.show();
                break;
        }
    }

}
